package com.tt.fashionWebSite.service.imp;

import com.tt.fashionWebSite.dao.*;
import com.tt.fashionWebSite.domain.*;
import com.tt.fashionWebSite.dto.*;
import com.tt.fashionWebSite.service.Ad_ProductDetailService;
import com.tt.fashionWebSite.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.ArrayList;
import java.util.List;

@Service("ad_productDetailService")
public class Ad_ProductDetailServiceImp extends GenericService implements Ad_ProductDetailService {
    @Autowired
    @Qualifier("chiTietSanPham")
    private ChiTietSanPhamDao chiTietSanPhamDao;

    @Autowired
    @Qualifier("loaiDanhMucSanPham")
    private LoaiDanhMucSanPhamDao loaiDanhMucSanPhamDao;

    @Autowired
    @Qualifier("mauSac")
    private MauSacDao mauSacDao;

    @Autowired
    @Qualifier("kichThuoc")
    private KichThuocDao kichThuocDao;

    @Autowired
    @Qualifier("hoaTiet")
    private HoaTietDao hoaTietDao;

    @Autowired
    @Qualifier("sanPham")
    private SanPhamDao sanPhamDao;

    @Autowired
    @Qualifier("sanPhamFull")
    private SanPhamFullDao sanPhamFullDao;

    @Autowired
    @Qualifier("hinhAnhSanPham")
    private HinhAnhSanPhamDao hinhAnhSanPhamDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int addProductDetail(ChiTietSanPhamDto productDetailDto) {
        ChiTietSanPham chiTietSanPham = chiTietSanPhamDao.findById(productDetailDto.getMaCTSP());
        if(chiTietSanPham == null){
            chiTietSanPham = new ChiTietSanPham();
            chiTietSanPham.setMaCTSP(productDetailDto.getMaCTSP());
            chiTietSanPham.setGiaSP(productDetailDto.getGiaSP());
            chiTietSanPham.setSoLuong(productDetailDto.getSoLuong());

            KichThuoc kichThuoc = kichThuocDao.findById(productDetailDto.getKichThuoc().getMaSize());
            chiTietSanPham.setKichThuoc(kichThuoc);

            MauSac mauSac = mauSacDao.findById(productDetailDto.getMauSac().getMaMau());
            chiTietSanPham.setMauSac(mauSac);

            HoaTiet hoaTiet = hoaTietDao.findById(productDetailDto.getHoaTiet().getMaHoaTiet());
            chiTietSanPham.setHoaTiet(hoaTiet);

            SanPham sanPham = sanPhamDao.findById(productDetailDto.getSanPham().getMaSP());
            chiTietSanPham.setSanPham(sanPham);

            chiTietSanPhamDao.insert(chiTietSanPham);
            return JsonResponseCode.SUCCESS;
        }
        else {
            return JsonResponseCode.FAILED;
        }
    }

    public List<ChiTietSanPhamDto> getAllProductDetail () {
        return mapper.mapCollection(chiTietSanPhamDao.getAllProductDetail(), ChiTietSanPhamDto.class);
    }

    public List<LoaiDanhMucSanPhamDto> getAllProductList () {
        return mapper.mapCollection(loaiDanhMucSanPhamDao.getAllProductList(), LoaiDanhMucSanPhamDto.class);
    }

    public List<MauSacDto> getAllColor () {
        return mapper.mapCollection(mauSacDao.getAllColor(), MauSacDto.class);
    }

    public List<KichThuocDto> getAllSize () {
        return mapper.mapCollection(kichThuocDao.getAllSize(), KichThuocDto.class);
    }

    public List<HoaTietDto> getAllMotif () {
        return mapper.mapCollection(hoaTietDao.getAllMotif(), HoaTietDto.class);
    }

    public List<SanPhamDto> getAllProduct () {
        return mapper.mapCollection(sanPhamDao.getAllProduct(), SanPhamDto.class);
    }

    @Override
    public SanPhamFullDto getDataDetailOfDataCommon(String productId){
        List<ChiTietSanPhamDto> chiTietSanPhamDtos = new ArrayList<ChiTietSanPhamDto>();
        List<ChiTietSanPham> chiTietSanPhams = chiTietSanPhamDao.getDataDetailOfDataCommon(productId);
        chiTietSanPhamDtos = mapper.mapCollection(chiTietSanPhams, ChiTietSanPhamDto.class, CHI_TIET_SAN_PHAM_MAP_ID);

        SanPhamFull sanPhamFull = sanPhamFullDao.findById(productId);
        SanPhamFullDto sanPhamFullDto = mapper.map(sanPhamFull, SanPhamFullDto.class, SAN_PHAM_FULL_MAP_ID);
        return sanPhamFullDto;
    }

    @Override
    public SanPhamFullDto getDataDetailForDeal(String color, String motif, String size){
        List<SanPhamFullDto> sanPhamFullDtos = new ArrayList<SanPhamFullDto>();
        List<SanPhamFull> sanPhamFull = sanPhamFullDao.getDataDetailForDeal(color, motif, size);
        SanPhamFullDto sanPhamFullDto = mapper.map(sanPhamFull, SanPhamFullDto.class, SAN_PHAM_FULL_MAP_ID);
        return sanPhamFullDto;
    }

    public ChiTietSanPhamDto getInformationOfProductDetail(ChiTietSanPhamDto productDto){
        ChiTietSanPham chiTietSanPham = chiTietSanPhamDao.findById(productDto.getMaCTSP());
        ChiTietSanPhamDto chiTietSanPhamDto = mapper.map(chiTietSanPham, ChiTietSanPhamDto.class,CHI_TIET_SAN_PHAM_MAP_ID);
        return  chiTietSanPhamDto;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public ChiTietSanPhamDto updateProductDetail(ChiTietSanPhamDto productDto) {
        ChiTietSanPham chiTietSanPham = chiTietSanPhamDao.findById(productDto.getMaCTSP());
        try {
            chiTietSanPham.setMaCTSP(productDto.getMaCTSP());
            chiTietSanPham.setGiaSP(productDto.getGiaSP());
            chiTietSanPham.setSoLuong(productDto.getSoLuong());

            KichThuoc kichThuoc = kichThuocDao.findById(productDto.getKichThuoc().getMaSize());
            chiTietSanPham.setKichThuoc(kichThuoc);

            MauSac mauSac = mauSacDao.findById(productDto.getMauSac().getMaMau());
            chiTietSanPham.setMauSac(mauSac);

            HoaTiet hoaTiet = hoaTietDao.findById(productDto.getHoaTiet().getMaHoaTiet());
            chiTietSanPham.setHoaTiet(hoaTiet);

            SanPham sanPham = sanPhamDao.findById(productDto.getSanPham().getMaSP());
            chiTietSanPham.setSanPham(sanPham);
            sanPhamDao.update(sanPham);
            return productDto;
        }
        catch (Exception ex) {
            return null;
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Integer deleteProductDetail(List<ChiTietSanPhamDto> chiTietSanPhamDtos) {
        try{
            for(int i = 0; i < chiTietSanPhamDtos.size(); i++) {
                hinhAnhSanPhamDao.deletePicture(chiTietSanPhamDtos.get(i).getMaCTSP());
                chiTietSanPhamDao.deleteProductDetail(chiTietSanPhamDtos.get(i).getMaCTSP());
            }
            return JsonResponseCode.SUCCESS;
        }
        catch (Exception ex) {
            return JsonResponseCode.FAILED;
        }
    }
}
